home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Original Code / Buttons and Labels and Scrolls (Oh, My!) / ColorScroll / ColorScroll.cs next >
Encoding:
Text File  |  2001-01-15  |  3.3 KB  |  97 lines

  1. //------------------------------------------
  2. // ColorScroll.cs ⌐ 2001 by Charles Petzold
  3. //------------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Windows.Forms;
  7.  
  8. class ColorScroll: Form
  9. {
  10.      Panel        panel;
  11.      Label[]      alabelName  = new Label[3];
  12.      Label[]      alabelValue = new Label[3];
  13.      VScrollBar[] avscroll    = new VScrollBar[3];
  14.  
  15.      public static void Main()
  16.      {
  17.           Application.Run(new ColorScroll());
  18.      }
  19.      public ColorScroll()
  20.      {
  21.           Text = "Color Scroll";
  22.  
  23.           Color[] acolor = { Color.Red, Color.Green, Color.Blue };
  24.  
  25.                // Create the panel.
  26.  
  27.           panel = new Panel();
  28.           panel.Parent = this;
  29.           panel.Location = new Point(0, 0);
  30.           panel.BackColor = Color.White;
  31.  
  32.                // Loop through the three colors.
  33.  
  34.           for (int i = 0; i < 3; i++)
  35.           {
  36.                alabelName[i] = new Label();
  37.                alabelName[i].Parent = panel;
  38.                alabelName[i].ForeColor = acolor[i];
  39.                alabelName[i].Text = "&" + acolor[i].ToKnownColor();
  40.                alabelName[i].TextAlign = ContentAlignment.MiddleCenter;
  41.  
  42.                avscroll[i] = new VScrollBar();
  43.                avscroll[i].Parent = panel;
  44.                avscroll[i].SmallChange = 1;
  45.                avscroll[i].LargeChange = 16;
  46.                avscroll[i].Minimum  = 0;
  47.                avscroll[i].Maximum = 255 + avscroll[i].LargeChange - 1;
  48.                avscroll[i].ValueChanged += 
  49.                               new EventHandler(ScrollOnValueChanged);
  50.                avscroll[i].TabStop = true;
  51.  
  52.                alabelValue[i] = new Label();
  53.                alabelValue[i].Parent = panel;
  54.                alabelValue[i].TextAlign = ContentAlignment.MiddleCenter;
  55.           }
  56.           Color color = BackColor;
  57.           avscroll[0].Value = color.R;  // Generates ValueChanged event.
  58.           avscroll[1].Value = color.G;
  59.           avscroll[2].Value = color.B;
  60.  
  61.           OnResize(EventArgs.Empty);
  62.      }
  63.      protected override void OnResize(EventArgs ea)
  64.      {
  65.           base.OnResize(ea);
  66.  
  67.           int cx = ClientSize.Width;
  68.           int cy = ClientSize.Height;
  69.           int cyFont = Font.Height;
  70.  
  71.           panel.Size = new Size(cx / 2, cy);
  72.  
  73.           for (int i = 0; i < 3; i++)
  74.           {
  75.                alabelName[i].Location = new Point(i * cx / 6, cyFont / 2);
  76.                alabelName[i].Size = new Size(cx / 6, cyFont);
  77.  
  78.                avscroll[i].Location = new Point((4 * i + 1) * cx / 24,
  79.                                                 2 * cyFont);
  80.                avscroll[i].Size = new Size(cx / 12, cy - 4 * cyFont);
  81.  
  82.                alabelValue[i].Location = new Point(i * cx / 6,
  83.                                                    cy - 3 * cyFont / 2);
  84.                alabelValue[i].Size = new Size(cx / 6, cyFont);
  85.           }
  86.      }
  87.      void ScrollOnValueChanged(Object obj, EventArgs ea)
  88.      {
  89.           for (int i = 0; i < 3; i++)
  90.                if((VScrollBar) obj == avscroll[i])
  91.                     alabelValue[i].Text = avscroll[i].Value.ToString();
  92.  
  93.           BackColor = Color.FromArgb(avscroll[0].Value, 
  94.                                      avscroll[1].Value,
  95.                                      avscroll[2].Value);
  96.      }
  97. }